package com.ruoyi.web.controller.acitviti;

import com.github.pagehelper.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.workFlow.domain.ActTaskDTO;
import com.ruoyi.workFlow.domain.dto.ActWorkflowFormDataDTO;
import com.ruoyi.workFlow.service.ITaskService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * @ClassName TaskController
 * @Description 任务controller
 * @Author leizhang
 * Date 2021/10/28 4:32 下午
 * @Version 1.0
 **/
@RequestMapping("/task")
@RestController
public class TaskController extends BaseController {

    private static Logger LOGGER = LoggerFactory.getLogger(TaskController.class);
    @Autowired
    private ITaskService taskService;

    /**
     * @param requestMap
     * @return com.ruoyi.common.core.page.TableDataInfo
     * @Description : 获取个人待办任务
     * @Author : leizhang
     * @Date 4:43 下午 2021/10/28
     **/
    @GetMapping(value = "/list")
    public TableDataInfo getTasks(@RequestParam Map<String, Object> requestMap) {
        Page<ActTaskDTO> taskList = taskService.getTaskList(requestMap);
        return getDataTable(taskList);
    }

    /**
     * @param taskID
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @Description : 审批表单详情信息渲染
     * @Author : leizhang
     * @Date 2:43 下午 2021/10/29
     **/
    @GetMapping(value = "/formDataShow/{taskID}")
    public AjaxResult formDataShow(@PathVariable("taskID") String taskID) {
        return AjaxResult.success(taskService.formDataShow(taskID));
    }

    /**
     * @param taskID
     * @param formData
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @Description : 审批表单保存
     * @Author : leizhang
     * @Date 2:43 下午 2021/10/29
     **/
    @PostMapping(value = "/formDataSave/{taskID}/{processType}")
    public AjaxResult formDataSave(@PathVariable("taskID") String taskID,
                                   @PathVariable("processType") Integer processType,
                                   @RequestBody List<ActWorkflowFormDataDTO> formData) throws ParseException {

        switch (processType) {
            case 1:
                //运营需求单
                return toAjax(taskService.formDataSave(taskID, formData, 1));

            case 2:
                //临时工考勤
                return toAjax(taskService.tempsFlowSave(taskID, formData, 2));
            default:
                return AjaxResult.success();
        }


    }

    /**
     * @param requestParam {"taskId":"","reason":""}
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @Description : 任务撤销
     * @Author : leizhang
     * @Date 9:52 下午 2021/11/4
     **/
    @PostMapping("/delProcessInstance")
    public AjaxResult delProcessInstance(@RequestBody Map<String, Object> requestParam) {
        try {
            taskService.delProcessInstance(requestParam);
            return AjaxResult.success();
        } catch (Exception e) {
            LOGGER.error("任务撤销失败：{}", e.getCause(), e);
            return AjaxResult.error();
        }
    }

    /**
     * @param
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @Description : 发起者任务查询(未完成任务)
     * @Author : leizhang
     * @Date 10:24 上午 2021/11/6
     **/
    @GetMapping("/getHistoryInfo")
    public TableDataInfo getHistoryByStarter(@RequestParam Map<String, Object> requestParam) throws Exception{
        try {
            Page<ActTaskDTO> processInfoByStart = taskService.getProcessInfoByStart(requestParam);
            return getDataTable(processInfoByStart);
        } catch (Exception e) {
            LOGGER.error("发起者任务查询失败:{}",e.getCause(),e);
            throw new Exception(e.getMessage());
        }
    }
}
